const path = require('path')

const HtmlWebpackPlugin=require('html-webpack-plugin');
const pkg = require('./package.json');

const isProd = process.env.NODE_ENV === 'production'

console.log('start-----at', Date.now())
console.log('start-----isProd', isProd)
const jsxLoader = {
  test: /\.(js|jsx)$/,
  use: 'babel-loader',
  exclude: [/node_modules/, path.resolve('src/wd')],
}
const lessLoader = {
  test: /\.less$/,
  use: [{
    loader: "style-loader", // creates style nodes from JS strings
  }, {
    loader: "css-loader", // translates CSS into CommonJS
  }, {
    loader: "less-loader", // compiles Less to CSS,
    options: {
      lessOptions: {
        math: 'always'
      }
    }
  }],
}
const fileLoader = {
  test: /\.(png|jpg|gif)$/,
  use: [
    {
      loader: 'url-loader',
      options: {
        limit: 20000,
        esModule: false,
      },
    },
  ],
}


const config = {
  mode: isProd ? "production" : "development",
  entry: isProd ? path.resolve(__dirname, './src/index.js') : path.resolve(__dirname, './src/test.js'),
  output: {
    path: path.resolve(__dirname, isProd ? 'lib' : 'dist'),
    filename: "index.js",
  },
  module: {
    rules: [
      jsxLoader,
      lessLoader,
      fileLoader
    ],
  },
  externals: isProd ? { // 定义外部依赖，避免把react和react-dom打包进去
    react: {
      root: "React",
      commonjs2: "react",
      commonjs: "react",
      amd: "react"
    },
    "react-dom": {
      root: "ReactDOM",
      commonjs2: "react-dom",
      commonjs: "react-dom",
      amd: "react-dom"
    }
  } : {},
  plugins: isProd ? [] : [
    new HtmlWebpackPlugin({
      template: path.resolve('src/index.html'),
      filename: 'index.html',
      inject: true,
    })
  ],


}

if (!isProd) {
  config.devServer = {
    host: 'localhost',   //服务器的ip地址
      port: 1575,  //端口
      open: true,  //自动打开页面
  }
} else {
  config.output.libraryTarget = 'umd'
  // config.output.libraryExport = 'default'
  // config.output.library = ''

  // config.entry = path.resolve(__dirname, './src/index-build.js')
  // config.externals = {
  //   react: {
  //     root: "React",
  //     commonjs2: "react",
  //     commonjs: "react",
  //     amd: "react"
  //   },
  //   "react-dom": {
  //     root: "ReactDOM",
  //     commonjs2: "react-dom",
  //     commonjs: "react-dom",
  //     amd: "react-dom"
  //   }
  // }
}

// console.log('----config', config)

module.exports = config
